package com.hwd.srb.common.exception;

import com.hwd.srb.common.result.R;
import com.hwd.srb.common.result.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * @author 黄伟东/Victor
 * @date 2022/3/11 14:00
 * @Description 统一异常处理
 */
//@Component
//@RestControllerAdvice // 该类的所有方法将会以通知的形式被切入到所有restController中
@Slf4j
public class UnifiedExceptionHandler {

    /**
     * 通用异常处理
     *
     * @param e 异常对象
     * @return com.hwd.srb.common.result.R
     */
    @ExceptionHandler(Exception.class)
    public R handleException(Exception e) {
        System.out.println("==========通用异常============");
        System.out.println(e.getMessage());
        return R.error(e.getMessage());
    }

    /**
     * jdbc的sql异常处理
     *
     * @param e 异常对象
     * @return com.hwd.srb.common.result.R
     */
    @ExceptionHandler(BadSqlGrammarException.class)
    public R handleBadSqlGrammarException(BadSqlGrammarException e) {
        System.out.println("==========jdbc的sql异常处理============");
        System.out.println(e.getMessage());
        return R.error(e.getMessage());
    }

    /**
     * 自定义异常处理
     *
     * @param e 异常对象
     */
    @ExceptionHandler(BusinessException.class)
    public R handleBusinessException(BusinessException e) {
        System.out.println("==========自定义异常处理============");
        System.out.println(e.getMessage());
        return R.error().message(e.getMessage()).code(e.getCode());
    }

    /**
     * Controller上一层相关异常
     *
     * @param e 异常对象
     */
    @ExceptionHandler({
            NoHandlerFoundException.class,
            HttpRequestMethodNotSupportedException.class,
            HttpMediaTypeNotSupportedException.class,
            MissingPathVariableException.class,
            MissingServletRequestParameterException.class,
            TypeMismatchException.class,
            HttpMessageNotReadableException.class,
            HttpMessageNotWritableException.class,
            MethodArgumentNotValidException.class,
            HttpMediaTypeNotAcceptableException.class,
            ServletRequestBindingException.class,
            ConversionNotSupportedException.class,
            MissingServletRequestPartException.class,
            AsyncRequestTimeoutException.class
    })
    public R handleServletException(Exception e) {
        log.error(e.getMessage(), e);
        //SERVLET_ERROR(-102, "servlet请求异常"),
        return R.error().message(ResponseEnum.SERVLET_ERROR.getMessage()).code(ResponseEnum.SERVLET_ERROR.getCode());
    }

}
